﻿//
//  Fansub Stat
//  Copyright (C) 2011 Victor Tereschenko (aka sibvic)
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FansubStat.Subtitles;

namespace FansubStat
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = "";
            if (args.Length == 1)
                directory = args[0];
            else
                directory = AppDomain.CurrentDomain.BaseDirectory;
            List<SubtitleData> data = new List<SubtitleData>();
            string[] patterns = new string[] { "*.ass", "*.srt" };
            foreach (string pattern in patterns)
            {
                string[] files = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    SubtitleData subtitle = SubtitleParser.Parse(file);
                    if (subtitle != null)
                        data.Add(subtitle);
                }
            }

            print(data);
        }

        private static void print(List<SubtitleData> data)
        {
            Console.WriteLine("Total subtitles parsed: {0}", data.Count);

            TimeSpan totalDialogsTime = new TimeSpan();
            TimeSpan longestDialogsTime = new TimeSpan();
            TimeSpan totalSubtitlesTime = new TimeSpan();
            TimeSpan longesSubtitle = new TimeSpan();
            int maxSubLengthDialogs = 0;
            int totalDialogs = 0;
            foreach (SubtitleData sub in data)
            {
                if (maxSubLengthDialogs < sub.Items.Count)
                    maxSubLengthDialogs = sub.Items.Count;
                totalDialogs += sub.Items.Count;

                TimeSpan subLength = new TimeSpan();
                SubtitleDialog earliestDialog = null;
                SubtitleDialog latestDialog = null;
                foreach (SubtitleDialog dialog in sub.Items)
                {
                    TimeSpan length = dialog.EndTime - dialog.StartTime;
                    subLength += length;
                    if (earliestDialog == null || earliestDialog.StartTime > dialog.StartTime)
                        earliestDialog = dialog;
                    if (latestDialog == null || latestDialog.EndTime < dialog.EndTime)
                        latestDialog = dialog;
                }
                if (earliestDialog == null)
                    continue;
                if (longestDialogsTime < subLength)
                    longestDialogsTime = subLength;
                totalDialogsTime += subLength;
                TimeSpan subLengthAsb = latestDialog.EndTime - earliestDialog.StartTime;
                if (longesSubtitle < subLengthAsb)
                    longesSubtitle = subLengthAsb;
                totalSubtitlesTime += subLengthAsb;
            }

            Console.WriteLine("Longest dialogs time: {0}", formatTime(longestDialogsTime));
            Console.WriteLine("Longest subtitle: {0}", formatTime(longesSubtitle));
            Console.WriteLine("Biggest subtitle: {0}", maxSubLengthDialogs);
            Console.WriteLine("Total dialogs: {0}", totalDialogs);
            Console.WriteLine("Total dialogs time: {0}", formatTime(totalDialogsTime));
            double movies = totalSubtitlesTime.TotalMinutes / 90;
            double ovas = totalSubtitlesTime.TotalMinutes / 60;
            double serials = totalSubtitlesTime.TotalMinutes / 40;
            double tvs = totalSubtitlesTime.TotalMinutes / 25;
            double moviesByDialogs = totalDialogs / 1200;
            double ovasByDialogs = totalDialogs / 700;
            double tvsByDialogs = totalDialogs / 300;
            string analogs = string.Format("{0} movies or {1} TV serail episodes or {2} anime OVAs or {3} animeTVs", movies.ToString("0.0"), serials.ToString("0.0"), ovas.ToString("0.0"), tvs.ToString("0.0"));
            string analogsDialogs = string.Format("{0} movies or {1} anime OVAs or {2} animeTVs",
                moviesByDialogs.ToString("0.0"), ovasByDialogs.ToString("0.0"), tvsByDialogs.ToString("0.0"));
            Console.WriteLine("Total subtitle time: {0}\nBy length: {1}\nBy dialogs: {2}", formatTime(totalSubtitlesTime), analogs, analogsDialogs);
        }

        private static string formatTime(TimeSpan time)
        {
            if (time.TotalDays > 1)
                return string.Format("{0}d {1}:{2}m", time.Days, time.Hours, time.Minutes);
            if (time.TotalHours > 1)
                return string.Format("{0}:{1}:{2}s", time.Hours, time.Minutes, time.Seconds);
            return string.Format("{0}:{1}.{2}s", time.Minutes, time.Seconds, time.Milliseconds);
        }
    }
}
